<?php
use Lib\ResLoader;
class App_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
    /**
     * @var Zend_Auth
     */
    protected $_auth = null;

    /**
     * @var Zend_Acl
     */
    protected $_acl = null;

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        return false;
        $this->setAuth(ResLoader::getResource('auth'));
        $this->setAcl(ResLoader::getResource('acl'));

        // Setting user role
        if ($this->_auth->hasIdentity()) {
            $role = $this->_auth->getIdentity()->getUser()->role;
        } else {
            $role = 'guest';
        }

        // Mapping to determine which Resource the current
        // request refers to (really simple for this example!)
        $resource = $request->controller;
        $privilege = $request->action;

        if (!$this->_acl->has($resource)) {
            $resource = null;
        }

        

        // ACL Access Check
        if (!$this->_acl->isAllowed($role, $resource, $privilege)) {
            if ($this->_auth->getStorage()->read()) {

            } else {
//                $front = Zend_Controller_Front::getInstance();
//                if(!$front->hasPlugin('Zend_Controller_Plugin_ErrorHandler')){
//                    $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100);
//                }
//
//                /**
//                 * @var Zend_Controller_Plugin_ErrorHandler
//                 */
//                $errorHandler = $front->getPlugin('Zend_Controller_Plugin_ErrorHandler');

//                $errorHandler->setRequest()
//                    if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('Zend_Controller_Plugin_ErrorHandler')) {
//            // Register with stack index of 100
//            require_once 'Zend/Controller/Plugin/ErrorHandler.php';
//            $this->_plugins->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100);
//        }
//                $request = $this->getRequest();
//
//                $errorHandler = new \stdClass();
//                $errorHandler->type = Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER;
//                $errorHandler->exception = new \Lib\Exception\Error404Exception();
//
////
////                $response = $this->getResponse();
////
////                $response->setException(new \Lib\Exception\Error404Exception());
//
//                $request->setModuleName('default');
//                $request->setControllerName('error');
//                $request->setActionName('error');
//                $request->setParam('exceptions', $errorHandler);
//
            }
        }
    }

    /**
     * @param \Zend_Acl $acl
     * @return void
     */
    public function setAcl($acl)
    {
        $this->_acl = $acl;
    }

    /**
     * @return \Zend_Acl
     */
    public function getAcl()
    {
        return $this->_acl;
    }

    /**
     * @param \Zend_Auth $auth
     * @return void
     */
    public function setAuth($auth)
    {
        $this->_auth = $auth;
    }

    /**
     * @return \Zend_Auth
     */
    public function getAuth()
    {
        return $this->_auth;
    }
}
